import { Component, OnInit } from '@angular/core';
import { FileUploader, FileItem } from 'ng2-file-upload';
import { UserService } from '../../services/user.service';
import { NzMessageService } from 'ng-zorro-antd';
import { User } from '../../entity/User';

@Component({
  selector: 'app-fileupload',
  templateUrl: './fileupload.component.html',
  styleUrls: ['./fileupload.component.css']
})
export class FileuploadComponent implements OnInit {
  user: User;
  uploader: FileUploader;

  constructor(
      private userService: UserService
    , private message: NzMessageService
  ) { }

  ngOnInit() {
    this.user = this.userService.getUser();
    this.uploader = new FileUploader({
      url: '/api/UploadFile/upload',
      method: 'POST',
      itemAlias: 'file',
      autoUpload: false,
      additionalParameter: { 'user': this.user.name }
    });

    this.uploader.onCompleteAll = this.CompleteAll.bind(this);
  }

  upfile() {
    this.uploader.uploadAll();
  }

  CompleteAll() {
    this.uploader.clearQueue();
    this.message.info('文件上传成功');
  }
}
